package com.ruoyi.ysgl.yssb.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ysgl.yssb.domain.YsglYssbFj;
import com.ruoyi.ysgl.yssb.mapper.YsglYssbMapper;
import com.ruoyi.ysgl.yssb.domain.YsglYssb;
import com.ruoyi.ysgl.yssb.service.IYsglYssbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 预算申报Service业务层处理
 *
 * @author huacai
 * @date 2024-08-08
 */
@Service
public class YsglYssbServiceImpl implements IYsglYssbService
{
    @Resource
    private YsglYssbMapper ysglYssbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询预算申报
     *
     * @param yssbId 预算申报主键
     * @return 预算申报
     */
    @Override
    public YsglYssb selectYsglYssbByYssbId(String yssbId)
    {
        return ysglYssbMapper.selectYsglYssbByYssbId(yssbId);
    }

    /**
     * 查询预算申报列表
     *
     * @param ysglYssb 预算申报
     * @return 预算申报
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<YsglYssb> selectYsglYssbList(YsglYssb ysglYssb) {
        return ysglYssbMapper.selectYsglYssbList(ysglYssb);
    }

    /**
     * 新增预算申报
     *
     * @param ysglYssb 预算申报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYsglYssb(YsglYssb ysglYssb) {
        ysglYssb.setCreateTime(DateUtils.getNowDate());
        ysglYssb.setIdWhere("存在");
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ysglYssb.setDeptId(Math.toIntExact(loginUser.getUser().getDeptId()));
        int rows = ysglYssbMapper.insertYsglYssb(ysglYssb);
        insertYsglYssbFj(ysglYssb);
        return rows;
    }

    /**
     * 批量新增预算申报
     *
     * @param ysglYssbs 预算申报List
     * @return 结果
     */
    @Override
    public int batchInsertYsglYssb(List<YsglYssb> ysglYssbs)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ysglYssbs)) {
            try {
                for (int i = 0; i < ysglYssbs.size(); i++) {
                    int row = ysglYssbMapper.insertYsglYssb(ysglYssbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == ysglYssbs.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改预算申报
     *
     * @param ysglYssb 预算申报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYsglYssb(YsglYssb ysglYssb)
    {
//        ysglYssbMapper.deleteYsglYssbFjByYssbId(ysglYssb.getYssbId());
        insertYsglYssbFj(ysglYssb);
        return ysglYssbMapper.updateYsglYssb(ysglYssb);
    }

    /**
     * 批量删除预算申报
     *
     * @param yssbIds 需要删除的预算申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYsglYssbByYssbIds(String[] yssbIds)
    {
        ysglYssbMapper.deleteYsglYssbFjByYssbIds(yssbIds);
        return ysglYssbMapper.deleteYsglYssbByYssbIds(yssbIds);
    }

    /**
     * 删除预算申报信息
     *
     * @param yssbId 预算申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYsglYssbByYssbId(String yssbId)
    {
        ysglYssbMapper.deleteYsglYssbFjByYssbId(yssbId);
        return ysglYssbMapper.deleteYsglYssbByYssbId(yssbId);
    }

    /**
     * 新增附件
     * @param ysglYssbFj
     * @return
     */
    @Override
    public int insertYssbFjByYssbId(YsglYssbFj ysglYssbFj) {
        return ysglYssbMapper.insertYssbFjByYssbId(ysglYssbFj);
    }

    /**
     * 根据ID查询附件列表
     * @param ysglYssbFj
     * @return
     */
    @Override
    public List<YsglYssbFj> selectYssbFjList(YsglYssbFj ysglYssbFj) {
        return ysglYssbMapper.selectYssbFjList(ysglYssbFj);
    }

    /**
     * 批量删除预算申报附件
     *
     * @param fjIds 需要删除的预算申报附件主键
     * @return 结果
     */
    @Override
    public int deleteYsglYssbFjByFjIds(Integer[] fjIds)
    {
        return ysglYssbMapper.deleteYsglYssbFjByFjIds(fjIds);
    }

    /**
     * 新增预算申报附件信息
     *
     * @param ysglYssb 预算申报对象
     */
    public void insertYsglYssbFj(YsglYssb ysglYssb)
    {
        List<YsglYssbFj> ysglYssbFjList = ysglYssb.getYsglYssbFjList();
        String yssbId = ysglYssb.getYssbId();
        if (StringUtils.isNotNull(ysglYssbFjList))
        {
            List<YsglYssbFj> list = new ArrayList<YsglYssbFj>();
            for (YsglYssbFj ysglYssbFj : ysglYssbFjList)
            {
                ysglYssbFj.setYssbId(yssbId);
                list.add(ysglYssbFj);
            }
//            if (list.size() > 0) {
//                ysglYssbMapper.batchYsglYssbFj(list);
//            }
        }
    }
}
